package cn.bluech.example;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.StringRes;


/**
 * Description:
 * Create by Codeant on 2018/12/9 0009
 */
public class App extends Application {
    private static App instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

    }

    /**
     * 简易消息提示框
     * @param info 弹出的消息
     */
    public static void toast(String info){
        if(null==instance)
            return;

        if(Looper.myLooper()==null){ // Toast需要有looper才能显示，否是会抛异常
            new Handler(Looper.getMainLooper()).post(
                    () -> Toast.makeText(instance, info, Toast.LENGTH_SHORT).show());
        }else
            Toast.makeText(instance, info, Toast.LENGTH_SHORT).show();
    }

    public static void toast(@StringRes int strId){
        if(null!=instance){
            toast(instance.getString(strId));
        }
    }
}
